package com.yao.mall.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.IdListRequest;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.shoppingCart.ShoppingCartAddRequest;
import com.yao.mall.model.dto.shoppingCart.ShoppingCartUpdateRequest;
import com.yao.mall.model.vo.ShoppingCartVO;
import com.yao.mall.service.ShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 购物车控制层
 * @date 2024/3/29 19:42
 */
@RestController
@RequestMapping("/shoppingCart")
@Tag(name = "购物车相关接口")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;


    @GetMapping("/get")
    @Operation(summary = "获取购物车信息")
    public Result<List<ShoppingCartVO>> getShoppingCart(){
        List<ShoppingCartVO> shoppingCartVOList = shoppingCartService.getShoppingCart();
        return  Result.success(shoppingCartVOList);
    }

    @PostMapping("/add")
    @Operation(summary = "加入购物车")
    public Result<Boolean> addGoodsToCart(@RequestBody ShoppingCartAddRequest cartAddRequest){
        if(cartAddRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Boolean aBoolean  = shoppingCartService.addGoodsToCart(cartAddRequest);
        return  Result.success(aBoolean);
    }

    @PostMapping("/addList")
    @Operation(summary = "批量添加进购物车（用于合并）")
    public Result<Boolean> addGoodsListToCart(@RequestBody List<ShoppingCartAddRequest> cartAddRequestList){
        if(CollectionUtils.isEmpty(cartAddRequestList)){
            //没有需要批量添加的数据直接返回null
            Result.success(true);
        }
        Boolean aBoolean  = shoppingCartService.addGoodsListToCart(cartAddRequestList);
        return  Result.success(aBoolean);
    }

    @PutMapping("/update")
    @Operation(summary = "更新购物车")
    public Result<Boolean> updateCartGoods(@RequestBody ShoppingCartUpdateRequest cartUpdateRequest){
        Boolean  aBoolean = shoppingCartService.updateCartGoods(cartUpdateRequest);
        return  Result.success(aBoolean);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "批量删除购物车中商品")
    public Result<Boolean> deleteCartGoods(@RequestBody IdListRequest idListRequest){
        if(idListRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Boolean  aBoolean = shoppingCartService.deleteCartGoods(idListRequest.getIds());
        return  Result.success(aBoolean);
    }


}
